if (!isServer) exitWith{};
//#define __FORCE_TRACE__
//#define __FORCE_DEBUG__
//#define __FORCE_INFO__
#define THIS_FILE "acv_export\main.sqf"
#include <tmf_constants.h>
#include <tmf_macros.h>
#include <ACV_constantes.h>

TRACE("Start");

0 spawn {
	private ["_timeout","_obj_arr","_obj","_obj_name","_owner","_obj_type","_type","_arr"];
	_timeout = time + 300;
	waituntil {sleep 0.5436; !isnil "ACV_FNCT_PutInStock" && time < _timeout};
	if (isnil "ACV_FNCT_PutInStock") exitWith {
		TRACE("ACV Not Detected");
	};
	
	if (isnil QGVAR(EXPORTING_OBJECT_BUSY)) then { GVAR(EXPORTING_OBJECT_BUSY) = false; };
	if (isnil QUOTE(EXPORT_VARIABLE)) then { EXPORT_VARIABLE = []; };
	
	while { true } do {
		waituntil {!GVAR(EXPORTING_OBJECT_BUSY)};
		GVAR(EXPORTING_OBJECT_BUSY) = true;
		
		if (count EXPORT_VARIABLE > 0) then {
			{
				_obj_arr = _x;
				_obj = GETVAL(_obj_arr,0,objNull);
				_obj_name = GETVAL(_obj_arr,1,"");
				_owner = GETVAL(_obj_arr,2,"");
				_obj_type = "";
				if (IS_OBJECT(_obj)) then {
					_obj_type = typeof _obj;
				};
				_type = switch (true) do {
					case (_obj_type isKindOf "Man"): { ACV_NET_MAKEUNITS };
					case (AND_ALSO((_obj_type isKindOf "AllVehicles"),(count (crew _obj) > 0))): { ACV_NET_MAKEVEHICLES };
					default { ACV_NET_MAKESIMPLEOBJECT };
				};
				if (_obj_name == "" && _obj_type != "") then { _obj_name = [_obj_type] call FFUNC(displayName); };

				_arr = switch (_type) do {
					case (ACV_NET_MAKEUNITS): { [_type,(group _obj), _obj_name, _owner] };
					case (ACV_NET_MAKEVEHICLES): { [_type,(group ((crew _obj) select 0)), _obj_name, _owner] };
					case (ACV_NET_MAKESIMPLEOBJECT) : { [_type, _obj, _obj_name, _owner] };
				};
				TRACE_1("Put in Stock",_arr);
			
				_arr call ACV_FNCT_PutInStock;
				[ format[localize "STR_TMF_CREATEDBY",_obj_name, _owner]] call ACV_FNCT_BroadcastMessage;
			} foreach EXPORT_VARIABLE;
			EXPORT_VARIABLE = [];
		};
		GVAR(EXPORTING_OBJECT_BUSY) = false;
		sleep 0.4567;
	};
};

TRACE("End");